#!/bin/bash
# init script for the openQRM collectd-plugin
#
# openQRM Enterprise developed by openQRM Enterprise GmbH.
#
# All source code and content (c) Copyright 2014, openQRM Enterprise GmbH unless specifically noted otherwise.
#
# This source code is released under the GNU General Public License version 2, unless otherwise agreed with openQRM Enterprise GmbH.
# The latest version of this license can be found here: src/doc/LICENSE.txt
#
# By using this software, you acknowledge having read this license and agree to be bound thereby.
#
#           http://openqrm-enterprise.com
#
# Copyright 2014, openQRM Enterprise GmbH <info@openqrm-enterprise.com>
#

OPENQRM_SERVER_BASE_DIR=$(pushd $(dirname $0)/../../../../.. > /dev/null; echo $PWD; popd > /dev/null)
. $OPENQRM_SERVER_BASE_DIR/openqrm/etc/openqrm-server.conf
. $OPENQRM_SERVER_BASE_DIR/openqrm/include/openqrm-functions
. $OPENQRM_SERVER_BASE_DIR/openqrm/include/openqrm-server-functions
. $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/collectd/include/openqrm-plugin-collectd-functions
. $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/collectd/etc/openqrm-plugin-collectd.conf
openqrm_server_get_config
export resource_openqrmserver=$OPENQRM_SERVER_IP_ADDRESS

COLLECTD_CONF="$OPENQRM_SERVER_BASE_DIR/openqrm/plugins/collectd/etc/collectd.conf"
COLLECTD_DATA_DIR="$OPENQRM_SERVER_BASE_DIR/openqrm/plugins/collectd/data"

function openqrm_plugin_collectd_start() {
	echo "Starting the openQRM collectd-plugin"
	kill -9 `ps ax | grep collectd | grep -v openqrm | grep -v grep | awk {' print $1 '}` 2>/dev/null
	openqrm_chkconfig del collectd
	# start collectd
	if [ -x /usr/sbin/collectdmon ]; then
		if /usr/sbin/collectdmon -P /var/run/collectdmon.pid -- -C $COLLECTD_CONF; then
			touch $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/base/plugins/collectd/.running
		fi
	fi
}


function openqrm_plugin_collectd_stop() {
	echo "Stopping the openQRM collectd-plugin"
	/etc/init.d/collectd stop
	# be sure it is stopped
	kill `ps ax | grep collectd | grep -v openqrm | grep -v grep | awk {' print $1 '}` 2>/dev/null
	# be sure it is gone
	kill -9 `ps ax | grep collectd | grep -v openqrm | grep -v grep | awk {' print $1 '}` 2>/dev/null
	rm -f $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/base/plugins/collectd/.running
}

function openqrm_plugin_collectd_uninstall() {
	local USERNAME=$1
	local PASSWORD=$2
	if [ "$USERNAME" == "" ] && [ "$PASSWORD" == "" ]; then
		echo -n "collectd: Please give a username to uninstall the plugin : "
		read USERNAME
		echo
		echo -n "collectd: Please give the password for user $USERNAME : "
		read PASSWORD
		echo
	fi
	echo "Uninstalling the openQRM collectd-plugin"
	openqrm_plugin_collectd_stop

	# remove from cron
	if [ -e /etc/cron.d/openqrm-collectd-graph ]; then
		rm -f /etc/cron.d/openqrm-collectd-graph
		if [ -x /etc/init.d/cron ]; then
			/etc/init.d/cron reload
		elif [ -x /etc/init.d/crond ]; then
			/etc/init.d/crond reload
		else
			echo "Could not find cron init script in /etc/init.d/. Please reload cron manually!"
		fi
	fi

	# remove data dir
	rm -rf $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/collectd/data
	rm -rf $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/collectd/web/graphs
	rm -f $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/base/plugins/collectd
	rm -f $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/boot-service/boot-service-collectd.tgz
	rm -rf $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/collectd/etc/collectd
}

function openqrm_plugin_collectd_init() {
	local USERNAME=$1
	local PASSWORD=$2
	if [ "$USERNAME" == "" ] && [ "$PASSWORD" == "" ]; then
		echo -n "collectd: Please give a username to init the plugin : "
		read USERNAME
		echo
		echo -n "collectd: Please give the password for user $USERNAME : "
		read PASSWORD
		echo
	fi

	# the collectd plugin depends on dns working so it needs the dns plugin available+enabled
	# collectd needs a hostname name. This we get from the dns-plugin config
	if [ ! -f $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/dns/etc/openqrm-plugin-dns.conf ]; then
		echo "ERROR: The openQRM collectd-plugin depends on the dns-plugin. Please enable the dns-plugin first!"
		openqrm_post_event 0 "collectd-plugin" 3 "collectd-plugin" "The openQRM collectd-plugin depends on the dns-plugin. Please enable the dns-plugin first!"
		exit 1
	fi
	. $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/dns/etc/openqrm-plugin-dns.conf
	echo "Initializing the openQRM collectd-plugin"

	# prepare collectd server config
	mkdir -p $COLLECTD_DATA_DIR
	cat $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/collectd/etc/templates/openqrm-collectd.conf | \
		sed -e "s/@@MODE@@/Listen/g" |  \
		sed -e "s#@@COLLECTD_DATA_DIR@@#$COLLECTD_DATA_DIR#g" |  \
		sed -e "s#@@OPENQRM_HOSTNAME@@#openqrm#g" |  \
		sed -e "s/@@OPENQRM_SERVER_IP_ADDRESS@@/$OPENQRM_SERVER_IP_ADDRESS/g" > $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/collectd/etc/collectd.conf

	# linking the web dir
	ln -sf $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/collectd/web $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/base/plugins/collectd
	# link the boot-service
	ln -sf $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/collectd/web/boot-service-collectd.tgz $OPENQRM_WEBSERVER_DOCUMENT_ROOT/openqrm/boot-service/boot-service-collectd.tgz
}

USER=$2
PASS=$3

case "$1" in
	start)
		openqrm_plugin_collectd_start
		;;
	stop)
		openqrm_plugin_collectd_stop
		;;
	restart)
		openqrm_plugin_collectd_stop
		sleep 1
		openqrm_plugin_collectd_start
		;;
	init)
		openqrm_plugin_collectd_init $USER $PASS
		;;
	uninstall)
		openqrm_plugin_collectd_uninstall $USER $PASS
		;;
	*)
		echo "Usage: $0 {start|stop|restart|init|uninstall}"
		exit 1

esac
exit $?




